import configparser
from conf.setting import FILE_PATH


class OperationConfig(object):
    """封装读取ini配置文件"""

    def __init__(self, file_path=None):
        if file_path is None:
            self.file_path = FILE_PATH['config']
        else:
            self.file_path = file_path

        self.conf = configparser.ConfigParser()
        try:
            self.conf.read(self.file_path, encoding='utf-8')
        except Exception as e:
            print(e)

    def get_section_for_data(self, section, option):
        """
        读取ini数据
        :param section:ini的头部值
        :param option: ini的选项值的key
        :return:
        """
        try:
            data = self.conf.get(section, option)
            return data
        except Exception as e:
            print(e)

    def get_envi(self,option):
        """获取接口服务器ip地址"""
        return self.get_section_for_data('api_envi', option)

    def get_mysql_conf(self,option):
         """获取mysql数据库配置信息"""
         return self.get_section_for_data('MYSQL', option)


if __name__ == '__main__':
    op = OperationConfig()
    print(op.get_envi( 'online'))
